package top.saplf.sileny.editor.edit

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import com.jakewharton.rxbinding2.view.RxView
import io.realm.Realm
import kotlinx.android.synthetic.main.activity_editor.*
import me.eugeniomarletti.extras.intent.IntentExtra
import me.eugeniomarletti.extras.intent.base.String
import top.saplf.sileny.R
import top.saplf.sileny.base.ui.IActivity
import top.saplf.sileny.editor.home.Note
import top.saplf.sileny.ext.statusBarTranslucent
import java.util.*

/**
 * @author saplf
 */
class EditorActivity : AppCompatActivity(), IActivity {

    companion object {
        var Intent.noteId: String? by IntentExtra.String()

        const val RESULT_CODE = 1
    }

    private val mRealm: Realm by lazy { Realm.getDefaultInstance() }
    private val mNote: Note by lazy {
        with(intent) {
            val note: Note
            if (noteId.isNullOrEmpty()) {
                note = Note(UUID.randomUUID().toString(), "", "", 0, 0)
                mRealm.beginTransaction()
                mRealm.insert(note)
                mRealm.commitTransaction()
            } else {
                note = mRealm.where(Note::class.java).equalTo(Note::id.name, noteId).findFirst()
            }
            note
        }
    }
    private val mCancelConfirmDlg: AlertDialog by lazy {
        AlertDialog.Builder(this@EditorActivity)
                .setMessage("是否放弃？")
                .setPositiveButton("确定") { dialog, _ ->
                    if (mNote.createTime == 0L) {
                        mRealm.beginTransaction()
                        mRealm.where(Note::class.java)
                                .equalTo(Note::id.name, mNote.id)
                                .findFirst()
                                .deleteFromRealm()
                        mRealm.commitTransaction()
                    }
                    finish()
                    dialog.dismiss()
                }
                .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                .create()
    }

    override fun onLayoutRes(): Int = R.layout.activity_editor

    override fun onViewCreated(savedInstanceState: Bundle?) {
        statusBarTranslucent(false)

        titleContent.setText(mNote.title ?: "")
        content.setText(mNote.content)

        RxView.clicks(saveEdit)
                .subscribe {
                    mRealm.beginTransaction()
                    mNote.title = titleContent.text.toString()
                    mNote.content = content.text.toString()
                    val time = System.currentTimeMillis()
                    mNote.modifyTime = time
                    if (mNote.createTime == 0L) {
                        mNote.createTime = time
                    }
                    mRealm.copyToRealmOrUpdate(mNote)
                    mRealm.commitTransaction()
                    setResult(RESULT_CODE, Intent().apply {
                        noteId = mNote.id
                    })
                    finish()
                }
        RxView.clicks(cancelEdit)
                .subscribe {
                    mCancelConfirmDlg.show()
                }
    }

    override fun onDestroy() {
        super.onDestroy()
        mRealm.close()
    }
}
